﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Meeting.DBUtility;
using Newtonsoft.Json;

namespace Meeting.BLL
{
    /// <summary>
    /// 模块功能：统计会议签到信息
    /// 模块作者：
    /// 模块日期：2013-01-27
    /// </summary>
    public class AttendanceStatistics
    {

        public string GetMeetingAttendance(int pageIndex, int pageSize, string filter)
        {
            int nAllRecord = 0, nAllPages = 0;
            DataTable dt = DbHelperSQL.GetPage("Id", @"  *                                                       
                                                      ,Isnull((Select Name  from dbo.Room Where Id=RoomId),'') As RoomName
                                                      ,Isnull((Select Name  from dbo.MeetingCategory Where Id=CategoryId),'') As CategoryName
                                                       ,(Select Count(*) From dbo.MeetingEmployee Where MeetingId = dbo.Meeting.Id) As EmpCount, 
                                               (Select Count(*) From dbo.MeetingEmployee Where MeetingId = dbo.Meeting.Id And LoginDate Is Not NULL) As FactCount ",
                                                     " dbo.Meeting ", filter, " CreateDate Desc ", pageIndex, pageSize, out nAllRecord, out nAllPages);
            if (dt == null) return "";
            Model.JsonGrid jr = new Model.JsonGrid();
            jr.total = nAllRecord;
            jr.rows = dt;
            Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm";
            return JsonConvert.SerializeObject(jr, timeConverter);
        }


        public string GetAttendanceDetail(string meetingId, bool flagAbsent)
        {
            string sql = "Select * From [MeetingEmployee] Where MeetingId = " + meetingId;
            if (flagAbsent)
            {
                sql += " And LoginDate Is Null ";
            }

            sql += " Order by LoginDate ";
                 
            DataSet ds = DbHelperSQL.Query(sql);
            if (ds == null) return "";
            Model.JsonGrid jr = new Model.JsonGrid();
            jr.total = ds.Tables[0].Rows.Count;
            jr.rows = ds.Tables[0];
            Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm";
            return JsonConvert.SerializeObject(jr, timeConverter);
        }
    }
}
